package com.example.chapter0704;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    public static final String RE = "re";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("num",1);
                startActivityForResult(intent,1001);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        TextView result = findViewById(R.id.tv_result);
        if (requestCode==1001 && resultCode == AppCompatActivity.RESULT_OK){
            if (data != null){
                String str = data.getStringExtra(RE);
                result.setText("\n 接收到的数据："+str);
            }else{

                result.setText("\n 没有接收到的数据");
            }
        }else  if (resultCode == AppCompatActivity.RESULT_CANCELED){
            result.setText("\n 没有");
        }
    }
}